package com.zwy.fornote.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zwy.fornote.dao.UserMapper;
import com.zwy.fornote.entity.req.NewUserReq;
import com.zwy.fornote.entity.req.PageReq;
import com.zwy.fornote.entity.req.UserManageReq;
import com.zwy.fornote.entity.vo.CountUserArticleVO;
import com.zwy.fornote.entity.vo.QuestionAndAnswerByUser;
import com.zwy.fornote.entity.vo.UserManageVO;
import com.zwy.fornote.model.User;
import com.zwy.fornote.service.UserService;
import com.zwy.fornote.util.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public int createNewUser(NewUserReq req) {
        User user=new User();
        user.setAccount(req.getAccount());
        user.setName(req.getName());
        user.setPassword(req.getPassword());
        user.setGender(req.getGender());
        user.setStatus(1);
        user.setIntroduce(req.getIntroduce());
        return userMapper.insertSelective(user);
    }

    @Override
    public User getUserByID(int id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public User getUserByAccountAndPassword(String account, String password) {
        User user = userMapper.getUserByAccountAndPassword(account, password);
        return user;
    }

    @Override
    public int lockUser(int id) {
        return userMapper.lockUserById(id);
    }

    @Override
    public int unlockUser(int id) {
        return userMapper.unlockUserById(id);
    }

    @Override
    public int checkUserStatus(int id) {
        return userMapper.checkUserStatus(id);
    }

    @Override
    public List<QuestionAndAnswerByUser> getUserAnswerAndQuestion(int useId) {
        return userMapper.getUserAnswerAndQuestion(useId);
    }

    @Override
    public int changeHeadPic(int userId, String headPic) {
        int i=userMapper.changeHeadPic(userId,headPic);
        //更换完头像后，要把当前存在工具类中的user的headPic属性也更新
        CurrentUser.getUser().setHeadpic(headPic);
        return i;
    }

    @Override
    public PageInfo<UserManageVO> selectManageUserList(UserManageReq req) {
        PageHelper.startPage(req.getPageNum(),req.getPageSize());
        List<UserManageVO> users = userMapper.manageUserList(req.getName(), req.getStatus());
        PageInfo<UserManageVO> pageInfo=new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public PageInfo<CountUserArticleVO> getCountUserArticle(PageReq req) {
        PageHelper.startPage(req.getPageNum(),req.getPageSize());
        List<CountUserArticleVO> users = userMapper.countUserArticle();
        PageInfo<CountUserArticleVO> pageInfo=new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public List<Integer> getAnalysis() {
        return userMapper.getAnalysis();
    }
}
